package com.zcx.qshop.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonTypelistAsyGet;

import java.util.List;

/**
 * Created by Administrator on 2015/8/28 0028.
 */
public class ThreeBarView extends HorizontalScrollView {

    @BoundView(R.id.three_bar_layout) private LinearLayout layout;

    private OnItemClickListener onItemClickListener;

    public ThreeBarView(Context context, AttributeSet attrs) {

        super(context, attrs);

        BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(context).inflate(R.layout.view_three_bar, this)));

    }

    public void setTypeThrees(List<JsonTypelistAsyGet.TypeTwo> typeTwos){

        if(typeTwos.size() > 0){

            int dividerCount = typeTwos.size() - 1;

            int titleWidth = (720 - (typeTwos.size() > 4 ? 3 : typeTwos.size() - 1))/ (typeTwos.size() > 4 ? 4 : typeTwos.size());

            for (int i = 0 ; i < typeTwos.size() ; i++ , dividerCount-- ){

                JsonTypelistAsyGet.TypeTwo typeTwo = typeTwos.get(i);

                Title title = new Title(getContext());

                title.setTag(typeTwo);

                title.setText(typeTwo.nexttile);

                title.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        setSelect((Title) v);

                    }

                });

                layout.addView(title);

                QSApplication.ScaleScreenHelper.loadViewWidthHeightSize(title, titleWidth, 91, 28);

                if(dividerCount > 0){

                    Divider divider = new Divider(getContext());

                    layout.addView(divider);

                    QSApplication.ScaleScreenHelper.loadViewWidthHeight(divider, 0, 76);

                }

            }

            setSelect((Title) layout.getChildAt(0));

        }else{

            setVisibility(View.GONE);

            onItemClickListener.onItemClick(new JsonTypelistAsyGet.TypeTwo());

        }

    }

    private void setSelect(Title title){

        for (int i = 0; i < layout.getChildCount() ; i ++){

            View view = layout.getChildAt(i);

            if(view instanceof Title){

                ((Title) view).setTextColor(Color.parseColor("#999999"));

            }

        }

        title.setTextColor(Color.parseColor("#3a9df5"));

        onItemClickListener.onItemClick((JsonTypelistAsyGet.TypeTwo) title.getTag());

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){

        this.onItemClickListener = onItemClickListener;

    }

    public interface OnItemClickListener{

        void onItemClick(JsonTypelistAsyGet.TypeTwo typeTwo);

    }

    private class Title extends TextView{

        public Title(Context context) {

            super(context);

            setGravity(Gravity.CENTER);

            setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        }

    }

    private class Divider extends View{

        public Divider(Context context) {

            super(context);

            setBackgroundResource(R.drawable.shape_divider_gray);

            setLayoutParams(new ViewGroup.LayoutParams(1, ViewGroup.LayoutParams.WRAP_CONTENT));

        }

    }

}
